package online.sixfish.codegenerator.enumeration;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EnumGenerator {

    static String PACKAGE="com.example.demo.enumeration";
    static String CLASS_NAME="YueActivityStatusEnum";

    static String[] enums = new String[]{
            "ActivityCanJoin&1&活动报名中",
            "ActivityJoinEnd&2&活动报名已结束",
            "JoinerFull&3&名额已满",
            "ActivityCancelled&4&人数未满，活动已自动取消",
            "WaitingActivityStart&5&等待活动开始",
            "ActivityRunning&6&活动进行中",
            "ActivityEnd&7&活动已结束"
    };

    public static void main(String[] args) {
        List<EnumItem> items = parseArgs(EnumGenerator.enums);
        EnumGenerator.generateEnum(items, PACKAGE, CLASS_NAME);
    }



    public static void generateEnum(List<EnumItem> items, String packageName, String className) {
        StringBuilder sb = new StringBuilder();

        sb.append("package ").append(packageName).append(";\n\n");
        sb.append("import java.util.ArrayList;\n" +
                "import java.util.Arrays;\n" +
                "import java.util.List;\n");
        sb.append("public enum ").append(className).append(" {\n");

        for (EnumItem item : items) {
            sb.append("\t").append(item.getName()).append("(").append(item.getId()).append(", \"").append(item.getDesc()).append("\"),\n");
        }

        sb.append("\n\t;\n\n");

        sb.append("\tprivate final int id;\n");
        sb.append("\tprivate final String desc;\n\n");

        sb.append("\t").append(className).append("(int id, String desc) {\n");
        sb.append("\t\tthis.id = id;\n");
        sb.append("\t\tthis.desc = desc;\n");
        sb.append("\t}\n\n");

        sb.append("\tpublic int getId() {\n");
        sb.append("\t\treturn id;\n");
        sb.append("\t}\n\n");

        sb.append("\tpublic String getDesc() {\n");
        sb.append("\t\treturn desc;\n");
        sb.append("\t}\n\n");

        sb.append("\tpublic static ").append(className).append(" getById(int id) {\n");
        sb.append("\t\tfor (").append(className).append(" status : ").append(className).append(".values()) {\n");
        sb.append("\t\t\tif (status.getId() == id) {\n");
        sb.append("\t\t\t\treturn status;\n");
        sb.append("\t\t\t}\n");
        sb.append("\t\t}\n");
        sb.append("\t\treturn null;\n");
        sb.append("\t}\n\n");

        sb.append("\tpublic static List<").append(className).append("> getAll").append(Character.toUpperCase(className.charAt(0))).append(className.substring(1)).append("() {\n");
        sb.append("\t\treturn new ArrayList<>(Arrays.asList(values()));\n");
        sb.append("\t}\n");

        sb.append("}");

        // Write the generated code to a file
        FileWriter writer = null;
        try {
            writer = new FileWriter(className + ".java");
            writer.write(sb.toString());
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private static List<EnumItem> parseArgs(String[] args) {
        List<EnumItem> items = new ArrayList<>();

        for (String arg : args) {
            String[] parts = arg.split("&");
            if (parts.length == 3) {
                String name = parts[0];
                int id = Integer.parseInt(parts[1]);
                String desc = parts[2];
                items.add(new EnumItem(name, id, desc));
            }
        }

        return items;
    }
}
